/* settings.c contains functions of settings */

#include "settings.hxx"

void saveSettings(void)
{
	FILE *settFile;
	if ((settFile = fopen(scro.configFile, "w")) != NULL)
	{
		scro.settings.id[0] = 'S';
		scro.settings.id[1] = 'C';
		scro.settings.id[2] = 'F';
		fwrite(&scro.settings, sizeof(scro.settings), 1, settFile);
		fclose(settFile);
	} else
	{
		logger.log(1, "couldn't open config file '%s' for writing.", scro.configFile);
	}
}

void loadSettings(void)
{
	FILE *settFile;
	if ((settFile = fopen(scro.configFile, "r")) != NULL)
	{
		fread(&scro.settings, sizeof(scro.settings), 1, settFile);
		if (!((scro.settings.id[0] == 'S') && (scro.settings.id[1] == 'C') && (scro.settings.id[2] == 'F')))
		{
			logger.log(1, "your config file is buggy, using defaults.");
			restoreDefaults();	
		}
		fclose(settFile);
	} else
	{
		logger.log(1, "couldn't open config file '%s' for reading.", scro.configFile);
	}
}


void restoreDefaults(void) /* start-up inits (defaults) */
{
	unsigned char objectCounter = 0;
	scro.settings.x = XMAX;
	scro.settings.y = YMAX;
	scro.settings.bpp = VIDEO_BITS;
	scro.settings.flags = VIDEO_MODE;
	scro.settings.p1ctrl = KBD1;

	scro.saveSettings = TRUE;
	/* sprintf(scro.settings.p1name, "player1"); */

	sprintf(scro.tempChar, "%s/%s", getenv("HOME"), SCRO_CONF);
	scro.configFile = new char[strlen(scro.tempChar) + 1];
	strcpy(scro.configFile, scro.tempChar);
	
	while (objectCounter < MAX_OBJECTS)
	{
		objectArray[objectCounter].id = 0;
		objectCounter++;
	}

	sprintf(scro.tempChar, "%s", "level.sli");
	scro.levelfile = new char[strlen(scro.tempChar) + 1];
	strcpy(scro.levelfile, scro.tempChar);
}

void setControls(void)
{
	switch(scro.settings.p1ctrl)
	{
		case KBD1:
		default:
			scro.p1Left = SDLK_LEFT;
			scro.p1Right = SDLK_RIGHT;
			scro.p1Up = SDLK_UP;
	   		scro.p1Down = SDLK_DOWN;	
	}
}

